<?php

namespace app\storeapi\controller\apps\sharp;

use app\storeapi\model\Category as CategoryModel;
use app\storeapi\controller\Controller;
use app\storeapi\model\sharp\ActiveTime as ActiveTimeModel;
use app\storeapi\model\sharp\Goods as SharpGoodsModel;

/**
 * 秒杀活动会场-场次管理
 * Class ActiveTime
 * @package app\store\controller\apps\sharp
 */
class ActiveTime extends Controller
{
    /**
     * 活动场次-场次列表
     * @param $active_id
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $param = $this->postData();
        $model = new ActiveTimeModel;
        $list = $model->getList($param);
        return $this->renderSuccess($list);
    }

    /**
     * 活动场次详情
     * @param $id
     * @return array|bool|mixed
     * @throws \think\exception\DbException
     * @throws \Exception
     */
    public function detail()
    {
        $param = $this->postData();
        // 场次详情
        $model = ActiveTimeModel::detail($param['active_time_id'], ['active']);
        if(!$model){
            return $this->renderError("该场次不存在，请刷新后重试");
        }
        // 当前场次关联的商品
        $goodsList = $model->getGoodsListByActiveTimeId($param);
        return $this->renderSuccess(compact('model','goodsList'));

    }

    /**
     * 删除活动场次
     * @param $id
     * @return array|bool
     * @throws \think\Exception
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function delete()
    {
        $param = $this->postData();
        // 场次详情
        $model = ActiveTimeModel::detail($param['active_time_id']);
        if(!$model){
            return $this->renderError("该场次不存在，请刷新后重试");
        }
        if (!$model->onDelete()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess([],'删除成功');
    }

    /**
     * 修改场次状态
     * @param $active_time_id
     * @param $status
     * @return array|bool
     * @throws \think\exception\DbException
     */
    public function status()
    {
        $param = $this->postData();
        // 场次详情
        $model = ActiveTimeModel::detail($param['active_time_id']);
        if(!$model){
            return $this->renderError("该场次不存在，请刷新后重试");
        }
        if (!$model->setStatus($param['status'])) {
            return $this->renderError('操作失败');
        }
        return $this->renderSuccess([],'操作成功');
    }

    /**
     * 删除活动场次
     * @param $id
     * @return array|bool
     * @throws \think\Exception
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function goodsDel()
    {
        $param = $this->postData();
        // 场次详情
        $model = ActiveTimeModel::detail($param['active_time_id']);
        if(!$model){
            return $this->renderError("该场次不存在，请刷新后重试");
        }
        if (!$model->goodsDel($param['goods_id'])) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess([],'删除成功');
    }


    /**
     * 活动场次新增商品
     * @param $id
     * @return array|bool|mixed
     * @throws \think\exception\DbException
     * @throws \Exception
     */
    public function goodsAdd()
    {
        $param = $this->postData();
        // 场次详情
        $model = ActiveTimeModel::detail($param['active_time_id']);
        if(!$model){
            return $this->renderError("该场次不存在，请刷新后重试");
        }
        // 新增记录
        if ($model->goodsAdd($param)) {
            return $this->renderSuccess([],'操作成功');
        }
        return $this->renderError($model->getError() ?: '操作失败');
    }


    /**
     * 活动场次新增商品
     * @param $id
     * @return array|bool|mixed
     * @throws \think\exception\DbException
     * @throws \Exception
     */
    public function goodsChos()
    {
        $param = $this->postData();
        // 场次详情
        $model = ActiveTimeModel::detail($param['active_time_id']);
        //获取已经添加的商品
        $goodsIds = $model->goodsIds($param['active_time_id']);
        //获取未添加秒杀的商品
        $param['goodsIds'] = $goodsIds;
        // 新增记录
        $list = (new SharpGoodsModel)->getSharpList($param);
        // 商品分类
        $catgory = CategoryModel::getCacheTree();
        return $this->renderSuccess(compact('list','catgory'));
    }




}